<?php

namespace App\Exports;

use App\Models\Withdraw;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithProperties;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class WithdrawExport extends DefaultValueBinder implements FromQuery, WithHeadings , WithMapping ,WithCustomValueBinder
{
	use Exportable;
	
	public function bindValue(Cell $cell, $value)
    {
        $column = $cell->getColumn();
        if (in_array( $column, ['C'])) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }

    public function query()
    {
    	return Withdraw::query()
    		->select('withdraws.*','users.real_name','users.cert_number')
    		->join('users','users.id','withdraws.user_id')
		    ->where('withdraws.method','sandpay')
		    ->where('withdraws.status','pending')
		    ->whereNotNull('cert_number');
    }
    
    public function headings(): array
    {
    	return [
		    '序号（选填）',
		    '收款方姓名（必填）',
		    '收款方银行卡号（必填）',
		    '金额（必填，单位：元）',
		    '附言（选填）',
		    '收款人手机号（选填）'
	    ];
    }

    public function map($withdraw): array
    {
	    $data = [
		    '',
		    $withdraw->real_name,
		    $withdraw->cert_number,
		    $withdraw->actual_amount,
		    $withdraw->id,
		    '',
	    ];
	    return $data;
    }
}
